package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    //查询
    @GetMapping("/getById")
    public AjaxResult getById(Long id){
        Review review = reviewService.getById(id);
        return AjaxResult.success(review);
    }
    //分页查询
    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(Review review){
        startPage();
        List<Review> reviewList = reviewService.getDataByPage(review);
        return getDataTable(reviewList);
    }

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable String name,
                               @PathVariable Integer age,
                               @PathVariable String sex){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        review.setCreateBy("1");
        review.setCreateTime(new Date());
        review.setUpdateBy("1");
        review.setUpdateTime(new Date());
        return toAjax(reviewService.insertReview(review));
    }

    @PostMapping("/saveData")
    public AjaxResult saveDataWithPost(@RequestBody Review review){
        review.setCreateBy("1");
        review.setCreateTime(new Date());
        review.setUpdateBy("1");
        review.setUpdateTime(new Date());
        return toAjax(reviewService.insertReview(review));
    }
    /**=========================================================删除数据=============================================*/

    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        return toAjax(reviewService.deleteReviewById(id));
    }


    /**=========================================================修改数据=============================================*/

    @PutMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        review.setUpdateBy("1");
        review.setUpdateTime(new Date());
        return toAjax(reviewService.updateReview(review));
    }
    /**=========================================================查询数据=============================================*/

}